package com.sanuo.pico.act.work;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.sanuo.pico.R;
import com.sanuo.pico.databinding.ActPartTimeBinding;
import com.sanuo.pico.event.ConditionState;
import com.sanuo.pico.event.Energy;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.event.LoadingEvent;
import com.sanuo.pico.event.P2Event;
import com.sanuo.pico.event.P3Event;
import com.sanuo.pico.event.P4Event;
import com.sanuo.pico.event.P5Event;
import com.sanuo.pico.event.ReadyState;
import com.sanuo.pico.it.SettingPage;
import com.sanuo.pico.utils.Command;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class PartAct extends BaseWorkActivity implements SettingPage {
    ActPartTimeBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActPartTimeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.title.setText(getResources().getStringArray(R.array.settings)[0]);
        binding.back.setOnClickListener(v -> Command.sendTouchEvent(0, 0x20));

        setTouch(binding.p1.down, () -> Command.sendTouchEvent(0, 54));
        setTouch(binding.p1.up, () -> Command.sendTouchEvent(0, 53));

        setTouch(binding.p2.down, () -> Command.sendTouchEvent(0, 56));
        setTouch(binding.p2.up, () -> Command.sendTouchEvent(0, 55));

        setTouch(binding.p3.down, () -> Command.sendTouchEvent(0, 112));
        setTouch(binding.p3.up, () -> Command.sendTouchEvent(0, 113));

        setTouch(binding.p4.down, () -> Command.sendTouchEvent(0, 116));
        setTouch(binding.p4.up, () -> Command.sendTouchEvent(0, 117));

        setTouch(binding.p5.down, () -> Command.sendTouchEvent(0, 118));
        setTouch(binding.p5.up, () -> Command.sendTouchEvent(0, 119));

        binding.save.setOnClickListener(v -> Command.sendTouchEvent(0, 0x43));
        binding.condition.setOnClickListener(v -> Command.sendTouchEvent(0, 0x3b));
        binding.ready.setOnClickListener(v -> Command.sendTouchEvent(0, 0x3c));

        onMessageEvent(Event.energy);
        onMessageEvent(Event.p2);
        onMessageEvent(Event.p3);
        onMessageEvent(Event.p4);
        onMessageEvent(Event.p5);
        onMessageEvent(Event.loading);
        onMessageEvent(Event.condition);
        onMessageEvent(Event.ready);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Energy event) {
        binding.p1.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(P2Event event) {
        binding.p2.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(P3Event event) {
        binding.p3.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(P4Event event) {
        binding.p4.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(P5Event event) {
        binding.p5.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LoadingEvent event) {
        if (event.getValue() == 1) {
            showLoading();
        } else {
            hideLoading();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ConditionState event) {
        binding.condition.setSelected(event.getValue() == 1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ReadyState event) {
        binding.ready.setSelected(event.getValue() == 1);
    }
}
